<script setup>
import {ref} from "vue";
import {useRouter} from "vue-router"
import { pcaTextArr } from "element-china-area-data";
import request  from '@/api/request'
let router = useRouter();

let emits=defineEmits(["ref-table"])
let supplierPrices=ref({})
let vendor=ref([])
let chanpin=ref([])

let onSubmit=()=>{
    supplierPrices.value.supplierPricesid="SP-"+new Date().getTime();
        request({
            url:"/supplierPrices",
            method:"post",
            data:supplierPrices.value
        }).then(res=>{
           emits("ref-table")
        })
}

let chaxungys=()=>{
    request({
        url: '/material/index',
        method: 'get',
        params: {
            current: 1,
            size: 1000,
            materialOrProduct:'物料'
        }
    }).then(res => {
        chanpin.value=res.data.records
    })
    request({
        //查询客户
        url:"/vendor/All",
        method:"get"
    }).then(res=>{
        vendor.value=res.data
    })
}
chaxungys();

let jisuan=()=>{
    if(supplierPrices.value.supplierpricesStandardunitprice!=null &&  supplierPrices.value.supplierpricesPurchaseunitprice!=null ){
        if(supplierPrices.value.supplierpricesStandardunitprice>supplierPrices.value.supplierpricesPurchaseunitprice){
            supplierPrices.value.supplierpricesDiscountrate=supplierPrices.value.supplierpricesPurchaseunitprice/supplierPrices.value.supplierpricesStandardunitprice;
        }else{
            supplierPrices.value.supplierpricesDiscountrate=0
        }
        
    }else{
        supplierPrices.value.supplierpricesDiscountrate=null
    }
    if(supplierPrices.value.supplierpricesPurchaseunitprice!=null &&  supplierPrices.value.supplierpricesNopurchaseunitprice!=null){
    supplierPrices.value.supplierpricesTax=supplierPrices.value.supplierpricesPurchaseunitprice-supplierPrices.value.supplierpricesNopurchaseunitprice;
    }else{
        supplierPrices.value.supplierpricesTax=null
    }
}
</script>
<template>
    <div>
        <el-card>
            <template #header>
                <el-form :model="supplierPrices" ref="form" :inline="false" class="card_wrap">
                    <el-form-item label="供应商：" prop="supplierpricesVendorid" class="c_item">
                            <el-select v-model="supplierPrices.supplierpricesVendorid" style="width: 150px;">
                                <el-option v-for="item in vendor" :label="item.vendorName" :value="item.vendorId">
                                </el-option>
                            </el-select>
                    </el-form-item>
                    <el-form-item label="产品：" prop="supplierpricesProductsid" class="c_item">
                            <el-select v-model="supplierPrices.supplierpricesProductsid" style="width: 150px;">
                                <el-option v-for="item in chanpin" :label="item.materialName" :value="item.materialId">
                                </el-option>
                            </el-select>
                    </el-form-item>
                    <el-form-item label="标准采购单价："  prop="supplierpricesStandardunitprice" class="c_item" >
                            <el-input-number v-model="supplierPrices.supplierpricesStandardunitprice" @change="jisuan" @blur="jisuan" :min="0" style="width: 150px"></el-input-number>
                    </el-form-item>
                    <el-form-item label="采购单价（含税）："  prop="supplierpricesPurchaseunitprice" class="c_item">
                            <el-input-number v-model="supplierPrices.supplierpricesPurchaseunitprice" @change="jisuan" @blur="jisuan" :min="0" style="width: 150px"></el-input-number>
                    </el-form-item>
                    <el-form-item label="折扣率："  prop="supplierpricesDiscountrate" class="c_item">
                            <el-input-number v-model="supplierPrices.supplierpricesDiscountrate" disabled :min="0" style="width: 150px"></el-input-number>%
                    </el-form-item>
                    <el-form-item label="采购单价（不含税）："  prop="supplierpricesNopurchaseunitprice" class="c_item">
                            <el-input-number v-model="supplierPrices.supplierpricesNopurchaseunitprice" @change="jisuan" @blur="jisuan" :min="0" style="width: 150px"></el-input-number>
                    </el-form-item>
                    <el-form-item label="增值税率："  prop="supplierpricesVatrate" class="c_item">
                            <el-input-number v-model="supplierPrices.supplierpricesVatrate" :min="0" style="width: 150px"></el-input-number>%
                    </el-form-item>
                    <el-form-item label="税额："  prop="supplierpricesTax" class="c_item">
                            <el-input-number v-model="supplierPrices.supplierpricesTax" disabled style="width: 150px"></el-input-number>
                    </el-form-item>
                    <el-form-item label="提交时间：" prop="supplierpricesSubmissiontime" class="c_item">
                            <el-date-picker v-model="supplierPrices.supplierpricesSubmissiontime"
                            value-format="YYYY-MM-DD" type="date" placeholder="选择日期时间">
                            </el-date-picker>
                    </el-form-item>
                </el-form>
           
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">立即添加</el-button>
                </el-form-item>
            </template>
        </el-card>
    </div>
</template>
<style scoped>

.card_wrap {
    width: 98%;
    display: flex;
    flex-wrap: wrap;
    justify-content:flex-start;
}
.c_item{
    flex:1;
    margin:0px 5px 15px 0;
    width: calc((100%-10px)/2); 
    min-width:calc((100% - 10px)/2);
    max-width:calc((100% - 10px)/2);
}
</style>